﻿using ASmile.Com.Com;
using ASmile.DBModel;
using ASmile.Extend;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASmile.Com.Da
{
    public class ComBLL : BLLBase, IComBLL
    {
        public ApiResult<RetLoginVerify> LoginVerify(ArgLoginVerify arg)
        {
            var user = DB.Query<Sys_User>(s => s.LoginName == arg.LoginName && s.LoginPwd == arg.LoginPwd && s.IsValid == true).ToEntity();
            if (user == null)
            {
                return RetErr<RetLoginVerify>("用户名或密码错误");
            }
            var userRole = DB.Query<Sys_RoleUser>(s => s.UserId == user.Id && s.RoleId == arg.RoleId).ToEntity();
            if (userRole == null)
            {
                return RetErr<RetLoginVerify>("角色无效");
            }
            var role = DB.Query<Sys_Role>(s => s.Id == arg.RoleId).ToEntity();
            if (role == null)
            {
                return RetErr<RetLoginVerify>("角色配置异常");
            }
            LoginUser userInfo = new LoginUser
            {
                UserId = user.Id,
                UserName = user.UserName,
                RoleId = role.Id,
                RoleName = role.RoleName,
                IsManager = user.IsManager,
                DeptId = "00",
                DeptName = "默认",
                ClientId = arg.ClentId //暂无校验
            };
            RetLoginVerify ret = new RetLoginVerify
            {
                UserInfo = userInfo
            };
            return RetOK(ret, "登陆成功");
        }

        public ApiResult<Sys_User> GetUserInfo()
        {
            var user = DB.Query<Sys_User>(s => s.Id == CurrUserId && s.IsValid == true).ToEntity();
            return RetOK(user);
        }

        public ApiResult SetUserInfo(Sys_User arg)
        {
            if (arg.UserName.IsEmpty())
            {
                return RetErr("用户名不能为空");
            }
            Sys_User user = new Sys_User
            {
                Id = CurrUserId,
                UserName = arg.UserName,
                MobileNumber = arg.MobileNumber
            };

            DB.Update(user);
            return RetOK("修改成功");
        }

        public ApiResultList<Sys_User> SearchUsers(ArgPageData arg)
        {
            return new UserManageBLL().GetList(arg);
        }
    }
}